using System;
using System.ComponentModel;
using System.Device.Location;
using System.Globalization;
namespace Microsoft.Phone.Controls.Maps.Design
{
	public class LocationConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return sourceType == typeof(string);
		}
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			string text = value as string;
			if (text != null)
			{
				string[] array = text.Split(new char[]
				{
					','
				});
				switch (array.Length)
				{
				case 2:
				{
					double latitude;
					double longitude;
					if (double.TryParse(array[0], 167, CultureInfo.get_InvariantCulture(), ref latitude) && double.TryParse(array[1], 167, CultureInfo.get_InvariantCulture(), ref longitude))
					{
						GeoCoordinate geoCoordinate = new GeoCoordinate();
						geoCoordinate.set_Latitude(latitude);
						geoCoordinate.set_Longitude(longitude);
						return geoCoordinate;
					}
					break;
				}
				case 3:
				{
					double latitude2;
					double longitude2;
					double altitude;
					if (double.TryParse(array[0], 167, CultureInfo.get_InvariantCulture(), ref latitude2) && double.TryParse(array[1], 167, CultureInfo.get_InvariantCulture(), ref longitude2) && double.TryParse(array[2], 167, CultureInfo.get_InvariantCulture(), ref altitude))
					{
						GeoCoordinate geoCoordinate2 = new GeoCoordinate();
						geoCoordinate2.set_Latitude(latitude2);
						geoCoordinate2.set_Longitude(longitude2);
						geoCoordinate2.set_Altitude(altitude);
						return geoCoordinate2;
					}
					break;
				}
				case 4:
				{
					double latitude3;
					double longitude3;
					double altitude2;
					if (double.TryParse(array[0], 167, CultureInfo.get_InvariantCulture(), ref latitude3) && double.TryParse(array[1], 167, CultureInfo.get_InvariantCulture(), ref longitude3) && double.TryParse(array[2], 167, CultureInfo.get_InvariantCulture(), ref altitude2))
					{
						GeoCoordinate geoCoordinate3 = new GeoCoordinate();
						geoCoordinate3.set_Latitude(latitude3);
						geoCoordinate3.set_Longitude(longitude3);
						geoCoordinate3.set_Altitude(altitude2);
						return geoCoordinate3;
					}
					break;
				}
				}
				throw new FormatException(ExceptionStrings.TypeConverter_InvalidLocationFormat);
			}
			throw new NotSupportedException(ExceptionStrings.TypeConverter_InvalidLocationFormat);
		}
	}
}
